package com.sdy.po;

import com.alibaba.fastjson.JSONObject;
import com.sdy.utils.Contact;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用于实时数据的循环队列
 */
@SuppressWarnings("all")
public class RealTimeDataQueue {

    //队列起点
    private Integer pointer = 0;
    //大小
    private Integer size;
    private Parm parm;
    private Double[] dataQueue;
    private String[] timeQueue;

    public Integer getPointer() {
        return pointer;
    }

    public void setPointer(Integer pointer) {
        this.pointer = pointer;
    }

    public void setSize(Integer size) {
        this.size = size;
    }


    public RealTimeDataQueue(Integer pointer, Integer size, Double[] dataQueue, String[] timeQueue) {
        this.pointer = pointer;
        this.size = size;
        this.dataQueue = dataQueue;
        this.timeQueue = timeQueue;
    }

    public Double[] getDataQueue() {
        return dataQueue;
    }

    public void setDataQueue(Double[] dataQueue) {
        this.dataQueue = dataQueue;
    }

    public String[] getTimeQueue() {
        return timeQueue;
    }

    public void setTimeQueue(String[] timeQueue) {
        this.timeQueue = timeQueue;
    }

    public RealTimeDataQueue() {
    }



    public RealTimeDataQueue(Integer size,Parm parm){
        this.size = size;
        this.parm = parm;
        this.dataQueue = new Double[size];
        this.timeQueue = new String[size];
    }

    //获得排列好的队列,并抽取时间和
    public JSONObject getSequenceQueueAndState(){
        JSONObject result = new JSONObject();
        List<Double> dataList = new ArrayList<>();
        List<String> timeList = new ArrayList<>();
        Integer stateNum = 0;
        Integer num = pointer;
        for (int i = 0; i < size; i++) {
            if (num == size){
                num = 0;
            }
            if (dataQueue[num] == null){
                timeList.add("--");
                dataList.add(null);
                num++;
                continue;
            }
            if (dataQueue[num] > parm.getMax() || dataQueue[num] < parm.getMin()){
                stateNum++;
            }
            timeList.add(timeQueue[num]);
            dataList.add(dataQueue[num]);
            num++;
        }
        JSONObject sequenceQueue = new JSONObject();
        sequenceQueue.put("timeList",timeList);
        sequenceQueue.put("datasList",dataList);
        result.put("sequenceQueue",sequenceQueue);
        result.put("dataState",getState(stateNum));
        return result;
    }

    //只获取状态
    public String getParmState(){
        Integer stateNum = 0;
        for (int i = 0; i < size; i++) {
            Double data = dataQueue[i];
            if (data == null){
                continue;
            }
            if (data > parm.getMax() || data < parm.getMin()){
                stateNum++;
            }
        }
        return getState(stateNum);
    }

    //获取当前设备运转状态达标情况
    private String getState(Integer stateNum){
        if (stateNum < size/3){
            return Contact.STATE_NORMAL;
        }
        else if (stateNum >= size/3 && stateNum < size*2/3){
            return Contact.STATE_WARNING;
        }
        return Contact.STATE_ERROR;
    }

    //向队列加入元素
    public void insertDataToQueue(JSONObject data){
        if (pointer == size){
            pointer = 0;
        }
        dataQueue[pointer] = Double.valueOf(data.get("data").toString());
        timeQueue[pointer] = data.get("time").toString().substring(11,20);
        pointer++;
    }

    public Integer getSize (){
        return dataQueue.length;
    }

}
